package yslai.divination;

import yslai.divination.R;
import android.app.*;
import android.graphics.Color;
import android.os.*;
import android.view.*;
import android.view.ViewGroup.LayoutParams;
import android.view.View.*;
import android.widget.*;
import android.util.*;

public class BaguaActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bagua);
		
		final ImageButton bagua_button = (ImageButton)findViewById(R.id.baguaButton);
		final TextView msg_view = (TextView)findViewById(R.id.msg);
		bagua_button.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				DivinationRound dr = new DivinationRound();
				
				final LinearLayout ll = (LinearLayout)findViewById(R.id.retArea);
				ll.removeAllViews();
				LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT,1);
				param.gravity = Gravity.CENTER;

				TextView origin_name = new TextView(getApplicationContext());
				origin_name.setText(dr.getOrigin().getName());
				origin_name.setTextColor(Color.WHITE);
				origin_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
				origin_name.setGravity(Gravity.CENTER);
				origin_name.setLayoutParams(param);
				ImageView origin = new ImageView(getApplicationContext());
				int oimgID = getResources().getIdentifier("yi"+dr.getOrigin().getId(), "drawable", getPackageName());
				origin.setImageResource(oimgID);
				origin.setLayoutParams(param);
				
				StringBuffer buf = new StringBuffer();
				if(dr.isHasMutation()){
					TextView and_name = new TextView(getApplicationContext());
					and_name.setText("之");
					and_name.setTextColor(Color.WHITE);
					and_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
					and_name.setGravity(Gravity.CENTER);
					and_name.setLayoutParams(param);
					
					TextView mutate_name = new TextView(getApplicationContext());
					mutate_name.setText(dr.getMutation().getName());
					mutate_name.setTextColor(Color.WHITE);
					mutate_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
					mutate_name.setGravity(Gravity.CENTER);
					mutate_name.setLayoutParams(param);
					ImageView mutate = new ImageView(getApplicationContext());
					int mimgID = getResources().getIdentifier("yi"+dr.getMutation().getId(), "drawable", getPackageName());
					mutate.setImageResource(mimgID);
					mutate.setLayoutParams(param);
					
					ll.addView(origin_name,LayoutParams.MATCH_PARENT);
					ll.addView(origin, LayoutParams.MATCH_PARENT);
					ll.addView(and_name, LayoutParams.MATCH_PARENT);
					ll.addView(mutate_name, LayoutParams.MATCH_PARENT);
					ll.addView(mutate, LayoutParams.MATCH_PARENT);
					
					if(dr.getMut_count() == 1 || dr.getMut_count() == 5 || dr.getMut_count() == 6){
						buf.append(dr.getResult_name()+"\n");
						buf.append(dr.getResult_txt()+"\n");
						buf.append(dr.getResult_desc());
					}else{
						buf.append(dr.getHue_name()+"\n");
						buf.append(dr.getHue_txt()+"\n");
						buf.append(dr.getHue_desc()+"\n");
						buf.append(dr.getJen_name()+"\n");
						buf.append(dr.getJen_txt()+"\n");
						buf.append(dr.getJen_desc());
					}
					//buf.append("卦象:"+dr.getOrigin().getName()+" "+dr.getOrigin().getdebug_yao()+"之"+dr.getMutation().getName()+" "+dr.getMutation().getdebug_yao()+" 變"+dr.getMut_count()+"爻");
				}else{
					ll.addView(origin_name,LayoutParams.MATCH_PARENT);
					ll.addView(origin, LayoutParams.MATCH_PARENT);
					buf.append(dr.getResult_name()+"\n");
					buf.append(dr.getResult_txt()+"\n");
					buf.append(dr.getResult_desc());
					//buf.append("卦象:"+dr.getOrigin().getName()+" "+dr.getOrigin().getdebug_yao());
				}
				msg_view.setText(buf.toString());
			}
		});
	}
}
